<template>
    <div class="login-wrap">
        <div class="ms-login">
            <div class="ms-title">学生成绩管理系统</div>
            <!-- <el-tabs :stretch="true" v-model="activeName">
        <el-tab-pane name="1">
          <span slot="label"><i class="el-icon-lx-group"></i> 学生登录</span>
          <el-form
            :model="param"
            :rules="rules"
            ref="login"
            label-width="0px"
            class="ms-content"
          >
            <el-form-item prop="userName">
              <el-input v-model="param.userName" placeholder="2020111">
                <el-button slot="prepend" icon="el-icon-lx-people"></el-button>
              </el-input>
            </el-form-item>
            <el-form-item prop="password">
              <el-input
                type="password"
                placeholder="password"
                v-model="param.password"
                @keyup.enter.native="submitForm()"
              >
                <el-button slot="prepend" icon="el-icon-lx-lock"></el-button>
              </el-input>
            </el-form-item>
            <div class="login-btn">
              <el-button type="primary" @click="submitForm()">登录</el-button>
            </div>
          </el-form>
        </el-tab-pane>
        <el-tab-pane name="2">
          <span slot="label"><i class="el-icon-lx-service"></i> 教师登录</span>
          <el-form
            :model="param"
            :rules="rules"
            ref="login"
            label-width="0px"
            class="ms-content"
          >
            <el-form-item prop="userName">
              <el-input v-model="param.userName" placeholder="202012">
                <el-button slot="prepend" icon="el-icon-lx-people"></el-button>
              </el-input>
            </el-form-item>
            <el-form-item prop="password">
              <el-input
                type="password"
                placeholder="password"
                v-model="param.password"
                @keyup.enter.native="submitForm()"
              >
                <el-button slot="prepend" icon="el-icon-lx-lock"></el-button>
              </el-input>
            </el-form-item>
            <div class="login-btn">
              <el-button type="primary" @click="submitForm()">登录</el-button>
            </div>
          </el-form>
        </el-tab-pane>
        <el-tab-pane name="3">
          <span slot="label"
            ><i class="el-icon-lx-profile"></i> 管理员登录</span
          >
          <el-form
            :model="param"
            :rules="rules"
            ref="login"
            label-width="0px"
            class="ms-content"
          >
            <el-form-item prop="userName">
              <el-input v-model="param.userName" placeholder="20203">
                <el-button slot="prepend" icon="el-icon-lx-people"></el-button>
              </el-input>
            </el-form-item>
            <el-form-item prop="password">
              <el-input
                type="password"
                placeholder="password"
                v-model="param.password"
                @keyup.enter.native="submitForm()"
              >
                <el-button slot="prepend" icon="el-icon-lx-lock"></el-button>
              </el-input>
            </el-form-item>
            <div class="verify">
              <verify
                style="float:left"
                :key="verify"
                @success="verifySuccess"
                @error="verifyError"
                :type="3"
                :showButton="false"
                :barSize="barSize"
              >
              </verify>
              <span @click="refresh" class="refresh">
                <i class="el-icon-refresh" ></i>
              </span>
            </div>
            <div class="login-btn">
              <el-button type="primary" @click="submitForm()">登录</el-button>
            </div>
          </el-form>
        </el-tab-pane>
      </el-tabs> -->
            <el-form :model="param" :rules="rules" ref="login" label-width="0px" class="ms-content">
                <el-form-item prop="userName">
                    <el-input v-model="param.userName" placeholder="username">
                        <el-button slot="prepend" icon="el-icon-lx-people"></el-button>
                    </el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input type="password" placeholder="password" v-model="param.password" @keyup.enter.native="submitForm()">
                        <el-button slot="prepend" icon="el-icon-lx-lock"></el-button>
                    </el-input>
                </el-form-item>
                <div class="verify">
                    <verify
                        style="float:left"
                        :key="verify"
                        @success="verifySuccess"
                        @error="verifyError"
                        :type="3"
                        :showButton="false"
                        :barSize="barSize"
                    >
                    </verify>
                    <span @click="refresh" class="refresh">
                        <i class="el-icon-refresh"></i>
                    </span>
                </div>
                <div class="login-btn">
                    <el-button type="primary" @click="submitForm()">登录</el-button>
                </div>
            </el-form>
        </div>
    </div>
</template>

<script>
import { postRequest } from '../../api/index';
import verify from 'vue2-verify';
export default {
    data: function() {
        return {
            param: {
                userName: '',
                password: '',
                // 默认为学生登录
                flag: 1,
                check: false
            },
            rules: {
                userName: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
                password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
            },
            barSize: {
                width: '260px',
                height: '40px'
            },
            activeName: '1',
            verify: ''
        };
    },
    methods: {
        submitForm() {
            // 表单验证
            var that = this;
            this.$refs.login.validate(valid => {
                if (valid) {
                    // 如果判断为管理员登录，需要验证
                    if (this.activeName == '3') {
                        if (this.check) {
                            this.postLogin();
                        } else {
                            this.$message.error('请通过验证！');
                            that.param.password = '';
                        }
                    } else {
                        // 学生或老师登录，无需验证
                        this.postLogin();
                    }
                } else {
                    this.$message.error('请输入账号和密码');
                    return false;
                }
            });
        },
        verifySuccess() {
            this.$message.success('验证成功！');
            this.check = true;
        },
        verifyError() {
            this.check = false;
        },
        postLogin() {
            postRequest(
                '/public/login',
                { userName: this.param.userName, password: this.param.password },
                global.CONTENT_TYPE_URLENCODED
            ).then(res => {
                console.log(res);
                if (res.code == 200) {
                    this.$message.success('登录成功');
                    // 1 学生、 2 老师、 3 管理员
                    let status = res.data.status;
                    let token = res.data.token;
                    this.$store.commit('setToken', { token: token, status: status });
                    this.$router.push('/personal');
                } else if (res.code == 402) {
                    this.$message.error('账号或密码错误！');
                    this.param.password = '';
                }
            });
        },
        refresh() {
            // 重新绑定key值，生成一个新的组件
            this.verify = new Date().getTime();
            this.check = false;
        }
    },
    watch: {
        activeName: function(newVal, oldVal) {
            this.param.flag = Number(newVal);
        }
    },
    components: {
        verify
    }
};
</script>

<style scoped>
.login-wrap {
    position: relative;
    width: 100%;
    height: 100%;
    background-image: url(../../assets/img/login-bg.jpg);
    background-size: 100%;
}
.ms-title {
    width: 100%;
    line-height: 50px;
    text-align: center;
    font-size: 20px;
    color: #fff;
    border-bottom: 1px solid #ddd;
}
.ms-login {
    position: absolute;
    left: 50%;
    top: 50%;
    width: 350px;
    margin: -190px 0 0 -175px;
    border-radius: 5px;
    background: rgba(255, 255, 255, 0.3);
    overflow: hidden;
}
.ms-content {
    padding: 30px 30px;
}
.login-btn {
    text-align: center;
}
.login-btn button {
    width: 100%;
    height: 36px;
    margin-bottom: 10px;
}
.login-tips {
    font-size: 12px;
    line-height: 30px;
    color: #fff;
}
.verify {
    height: 48px;
}
.refresh {
    line-height: 40px;
    margin-left: 5px;
    cursor: pointer;
}
</style>
